import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_example/get_put_find/controller/my_product_controller.dart';

class MyProductDetailPage extends StatelessWidget {
  //路由地址
  static const String route = "/MyProductDetailPage";

  const MyProductDetailPage({super.key});

  // https://dart.dev/tools/diagnostic-messages#unnecessary_non_null_assertion
  buildBackListTileRow(Map? val) {
    return val == null || val["id"] == null
        ? Container()
        : ListTile(
            title: Text("传值 id = ${val["id"]}"),
            subtitle: const Text('Get.back(result: {"success": true}'),
            onTap: () => Get.back(result: {"success": true}),
          );
  }

  @override
  Widget build(BuildContext context) {
    final MyProductController productController = Get.find<MyProductController>();

    Map? details = Get.arguments;
    Map? parameters = Get.parameters;
    String previousRoute = Get.previousRoute;
    return Scaffold(
      appBar: AppBar(
        title:  Text("GetX $previousRoute"),
        backgroundColor: context.theme.primaryColor,
        leading: GestureDetector(
            onTap: () {
              Get.back(result: {"success": true});
            },
            child: const Icon(Icons.arrow_back_ios)),
      ),
      body: Column(
        children: [
          ListTile(
            title: const Text("导航-返回- 使用Get.offAll过来的, 无法返回了"),
            subtitle: const Text('Get.back()'),
            onTap: () {
              Get.back();
            },
          ),
          buildBackListTileRow(details),
          buildBackListTileRow(parameters),
          ListTile(
            title: Text("传值 Get.arguments; = ${details.toString()} parameters == ${parameters.toString()}"),
            subtitle: const Text('Get.back(result: {"success": true}'),
            onTap: () => Get.back(result: {"success": true}),
          ),
          ListTile(
              title: Text("传值 Get.arguments; = ${details.toString()} parameters == ${parameters.toString()}"),
              subtitle: Obx(() => Text(productController.myProductList.length.toString())),
              onTap: () {
                productController.addProduct(MyProduct(name: 'iPhone 16', description: 'APPle 设备', price: 8199));
                productController.addCount();
              }),
          Card(
            margin: const EdgeInsets.only(left: 20, right: 20, bottom: 10, top: 10),
            child: GetX<MyProductController>(
                init: productController,
                builder: (MyProductController controller) {
                  return ListTile(
                      title: const Text("find 寻找 控制器的验证 productController 添加元素"),
                      subtitle: Column(
                        children: controller.myProductList
                            .map((element) => Text(
                                "data: ${element.name} ${element.price} ${element.description} x ${controller.count}"))
                            .toList(),
                      ),
                      onTap: () {
                        controller.addProduct(MyProduct(name: 'iPhone 16', description: 'APPle 设备', price: 8199));
                        controller.addCount();
                      });
                }),
          ),
          Card(
              margin: const EdgeInsets.only(left: 20, right: 20, bottom: 10, top: 10),
              child: GetX<MyProductController>(
                  init: productController,
                  builder: (MyProductController controller) {
                    return ListTile(
                        title: const Text("find 寻找 控制器的验证 productController 删除元素"),
                        subtitle: Column(
                          children: controller.myProductList
                              .map((element) => Text(
                                  "data: ${element.name} ${element.price} ${element.description} x ${controller.count}"))
                              .toList(),
                        ),
                        onTap: () {
                          controller.removeProduct(controller.myProductList[0]);
                          controller.addCount();
                        });
                  })),
        ],
      ),
    );
  }
}
